const store = require('store')

class Item {

	constructor (value, parent) {
		this.value = value
		this.parent = parent
	}

	get data () {
		return {
			value: this.value,
		}
	}

	remove () {
		this.parent.remove(this.value)
	}

}

class OptionStorage {

	constructor (name) {
		this.name = name
		this.data = store.get(
			name,
			[]
		)
	}
	get items () {
		return this.data.map(x => new Item(x.value, this))
	}

	remove (val) {
		this.data = this.data.filter(item => item.value !== val)
		this.save()
	}

	add (value) {
		this.data = this.data.filter(x => x.value !== value)
		this.data.unshift({
			value,
		})

		return this
	}

	save () {
		store.set(this.name, this.data)
	}

}

module.exports = OptionStorage